As computer software systems increase in sophistication, the cost of developing the software increases. To minimize the cost of software development, developers often share code. Prior development efforts use three types of code sharing: (1) source code sharing, (2) compiled code sharing, and (3) code sharing through inheritance.
Also, as computer software systems increase in sophistication, software users are faced with increasing complexity in the maintenance and extensibility of their computer systems. Each time a software vendor generates new or improved capabilities, a user who wishes to benefit must somehow incorporate these modifications. Prior systems generally require such a user to upgrade the software. forcing the user to reinstall at least part of the system. Or, prior systems require a software developer to plan in advance for future enhancements and install the necessary hooks in the original system to enable the loading of enhancements at some future time.
Moreover, if a user wishes to enhance the capabilities of a piece of currently owned software by adding capabilities produced by a different software vendor, the user is limited by what the software vendors planned in advance. Prior systems generally require that the two pieces of software be designed to work together and that at least one of the software pieces have knowledge of what capabilities the other provides. Thus, in prior systems, later modifications to code as well as the sharing of code must be accounted for in the software design.
Source and compiled code sharing have been widely used for many years. Source code sharing refers to the use of the same source code by various computer programs or by various versions of the same computer program. For example, a spreadsheet program typically includes source code to control the displaying of a spreadsheet. If a word processing program allows the embedding of a spreadsheet within a document, then the word processing program may use the same (or slightly modified) source code to display the embedded spreadsheet object. Source code sharing is typically used by a single developer who develops multiple computer programs. For competitive reasons, developers typically do not share their source code with other developers. Moreover, even if the developer does share source code, the recipient of source code typically modifies the source code and thus two versions of the source code are maintained.
Compiled code sharing refers to the use of the same compiled code by various computer programs. The compiled code is typically stored in a static or dynamic link library. Compiled code stored in a static link library is shared when a computer program is linked before execution. Compiled code stored in a dynamic link library is shared when a computer program is linked during execution. The developer of a spell checking program, for example, may share compiled code by compiling the program and storing the compiled code in a static link library. The static link library can then be distributed to developers of word processing programs who can link the compiled spell checking code into their word processing program. The developer of the spell checking program typically needs to modify the compiled code to meet special requirements of certain developers. These modifications tend to increase the complexity (and size) of the compiled code and may conflict with requirements of other recipients. Alternatively, the developer could distribute multiple versions of the static link library. However, the maintenance of multiple versions can be costly.
Object-oriented programming techniques employ a concept referred to as inheritance to allow the sharing of code. An overview of well-known object-oriented programming techniques is provided, since the present invention is described below using object-oriented programming. Two common characteristics of object-oriented programming languages are support for data encapsulation and data type inheritance. Data encapsulation refers to the binding of functions and data. Inheritance refers to the ability to declare a data type in terms of other data types.
In the C++ language, object-oriented techniques are supported through the use of classes. A class is a user-defined type. A class declaration describes the data members and function members of the class. For example, the following declaration defines data members and a function member of a class named CIRCLE.
class CIRCLE PA1 { public: PA1 } PA1 CIRCLE a, b; PA1 a.x=2; PA1 a.y=2; PA1 a.radius=1; PA1 b.x=4; PA1 b.y=5; PA1 b.radius=2; PA1 a.draw( ); PA1 b.draw( ); PA1 class CIRCLE_FILL: CIRCLE PA1 { public: PA1 class CIRCLE_1: CIRCLE { . . . }; PA1 class CIRCLE_2: CIRCLE { . . . }; PA1 class PATTERN: CIRCLE_1, CIRCLE_2{ . . . }; PA1 class CIRCLE_1: virtual CIRCLE { . . . }; PA1 class CIRCLE_2: virtual CIRCLE { . . . }; PA1 class PATTERN: CIRCLE_1, CIRCLE_2{ . . . }; PA1 class CIRCLE PA1 { public: PA1 }; PA1 class CIRCLE_FILL: CIRCLE PA1 { public: PA1 CIRCLE *c_ptr; PA1 c_ptr=&c; PA1 CIRCLE a; PA1 CIRCLE_FILL b; PA1 a.draw( ); PA1 b.draw( ); PA1 CIRCLE *c_ptr; PA1 c_ptr=&b; PA1 class ISpreadSheet PA1 { virtual void File( )=0; PA1 } PA1 class IBasic PA1 { virtual void File( )=0; PA1 } PA1 class IDatabase PA1 { virtual void Data( )=0; PA1 } PA1 class IUnknown PA1 { virtual HRESULT QueryInterface (REFIID iid, void **ppv)=0; PA1 } PA1 switch (iid) { PA1 virtual void Data( )=0; PA1 virtual void File( )=0; PA1 virtual void Edit( )=0; PA1 virtual void Formula( )=0; PA1 virtual void Format( )=0; PA1 virtual void GetCell (string RC, cell *pCell)=0; PA1 if (pSpreadsheet-&gt;QueryInterface("IDatabase", &pIDatabase)) PA1 // IDatabase supported PA1 else PA1 // IDatabase not supported PA1 static void CreateInstanceXX, (REFIID iid, void **ppv)=0;
int x, y; PA2 int radius; PA2 void draw( ); PA2 int pattern; PA2 void fill( ); PA2 int x, y; PA2 int radius; PA2 virtual void draw( ); PA2 int pattern; PA2 virtual void draw( ); PA2 virtual void Edit( )=0; PA2 virtual void Formula( )=0; PA2 virtual void Format( )=0: PA2 virtual void GetCell (string RC, cell *pCell)=0; PA2 virtual void Data( )=0; PA2 virtual void Edit( )=0; PA2 virtual void Formula( )=0; PA2 virtual void Format( )=0; PA2 virtual void GetCell (string RC, cell *pCell)=0; PA2 virtual ULONG AddRef( )=0; PA2 virtual ULONG Release ( )=0; PA2 case IID_IBasic: PA2 case IID_IDatabase: PA2 case IID_IUnknown: PA2 default: PA2 } PA2 if (ret=TRUE) {AddRef( );}; PA2 return ret;
Variables x and y specify the center location of a circle and variable radius specifies the radius of the circle. These variables are referred to as data members of the class CIRCLE. The function draw is a user-defined function that draws the circle of the specified radius at the specified location. The function draw is referred to as a function member of class CIRCLE. The data members and function members of a class are bound together in that the function operates on an instance of the class. An instance of a class is also called an object of the class.
In the syntax of C++, the following statement declares the objects a and b to be of type class CIRCLE.
This declaration causes the allocation of memory for the objects a and b. The following statements assign data to the data members of objects a and b.
The following statements are used to draw the circles defined by objects a and b.
A derived class is a class that inherits the characteristics--data members and function members--of its base classes. For example, the following derived class CIRCLE_FILL inherits the characteristics of the base class CIRCLE.
This declaration specifies that class CIRCLE_FILL includes ail the data and function members that are in class CIRCLE in addition to those data and function members introduced in the declaration of class CIRCLE_FILL, that is, data member pattern and function member fill. In this example, class CIRCLE_FILL has data members x, y, radius, and pattern and function members draw and fill. Class CIRCLE_FILL is said to "inherit" the characteristics of class CIRCLE. A class that inherits the characteristics of another class is a derived class (e.g., CIRCLE_FILL). A class that does not inherit the characteristics of another class is a primary (root) class (e.g., CIRCLE). A class whose characteristics are inherited by another class is a base class (e.g., CIRCLE is a base class of CIRCLE_FILL). A derived class may inherit the characteristics of several classes, that is, a derived class may have several base classes. This is referred to as multiple inheritance.
A derived class may specify that a base class is to be inherited virtually. Virtual inheritance of a base class means that only one instance of the virtual base class exists in the derived class. For example, the following is an example of a derived class with two nonvirtual base classes.
In this declaration class PATTERN inherits class CIRCLE twice nonvirtually through classes CIRCLE_1 and CIRCLE_2. There are two instances of class CIRCLE in class PATTERN.
The following is an example of a derived class with two virtual base classes.
The derived class PATTERN inherits class CIRCLE twice virtually through classes CIRCLE_1 and CIRCLE_2. Since the class CIRCLE is virtually inherited twice, there is only one object of class CIRCLE in the derived class PATTERN. One skilled in the art would appreciate virtual inheritance can be very useful when the class derivation is more complex.
A class may also specify whether its function members are virtual. Declaring that a function member is virtual means that the function can be overridden by a function of the same name and type in a derived class. In the following example, the function draw is declared to be virtual in classes CIRCLE and CIRCLE_FILL.
If a virtual function is declared without providing an implementation, then it is referred to as a pure virtual function. A pure virtual function is a virtual function declared with the pure specifier, "=0". If a class specifies a pure virtual function, then any derived class needs to specify an implementation for that function member before that function member may be invoked.
In order to access objects, the C++ language provides a pointer data type. A pointer holds values that are addresses of objects in memory. Through a pointer, an object can be referenced. The following statement declares variable c_ptr to be a pointer on an object of type class CIRCLE and sets variable c_ptr to hold the address of object c.
Continuing with the example, the following statement declares object a to be of type class CIRCLE and object b to be of type class CIRCLE_FILL.
The following statement refers to the function draw as defined in class CIRCLE.
Whereas, the following statement refers to the function draw defined in class CIRCLE_FILL.
Moreover, the following statements type cast object b to an object of type class CIRCLE and invoke the function draw that is defined in class CIRCLE_FILL.
c_ptr-&gt;draw( ); II CIRCLE_FILL::draw( )
Thus, the virtual function that is called is function CIRCLE_FILL::draw.
FIG. 1 is a block diagram illustrating typical data structures used to represent an object. An object is composed of instance data (data members) and member functions, which implement the behavior of the object. The data structures used to represent an object comprise instance data structure 101, virtual function table 102, and the function members 103, 104, 105. The instance data structure 101 contains a pointer to the virtual function table 102 and contains data members. The virtual function table 102 contains an entry for each virtual function member defined for the object. Each entry contains a reference to the code that implements the corresponding function member. The layout of this sample object conforms to the model defined in U.S. patent application Ser. No. 07/682,537, entitled "A Method for Implementing Virtual Functions and Virtual Bases in a Compiler for an Object Oriented Programming Language," which is hereby incorporated by reference. In the following, an object will be described as an instance of a class as defined by the Car programming language. One skilled in the art would appreciate that objects can be defined using other programming languages.
The inheritance of a class is a type of code sharing. A developer of a class can provide the implementation of the class to other developers. These other developers can then create classes that derive from the class provided. Thus, the function members of the provided class are shared. If, however, a class is inherited and a virtual function is overridden, then the testing of the overriding virtual function can be complex. The overriding virtual function can modify the state of the object in a way that affects non-overridden functions. Thus, each inherited function must be independently tested in conjunction with the testing of the overriding virtual function. To ameliorate the complexities of testing, the developers of a class implementation may distribute source code with the implementation. Unfortunately, the distribution of source code has the same drawbacks to sharing source code as discussed above.
An advantage of using object-oriented techniques is that these techniques can be used to facilitate the sharing of objects. In particular, object-oriented techniques facilitate the creation of compound documents. A compound document is a document that contains objects generated by various computer programs. (Typically, only the data members of the object and the class type are stored in a compound document.) For example, a word processing document that contains a spreadsheet object generated by a spreadsheet program is a compound document. A word processing program allows a user to embed a spreadsheet object (e.g., a cell) within a word processing document. To allow this embedding, the word processing program is compiled using the class definition of the object to be embedded to access function members of the embedded object. Thus, the word processing program would need to be compiled using the class definition of each class of objects that can be embedded in a word processing document. To embed an object of a new class into a word processing document the word processing program would need to be recompiled with the new class definition. Thus, only objects of classes selected by the developer of the word processing program can be embedded. Furthermore, new classes can only be supported with a new release of the word processing program.
To allow objects of an arbitrary class to be embedded into compound documents, interfaces are defined through which an object can be accessed without the need for the word processing program to have access to the class definitions at compile time. An abstract class is a class in which there is at least one virtual function member with no implementation (a pure virtual function member). An interface is an abstract class with no data members and whose virtual functions are all pure. Thus, an interface provides a protocol for two programs to communicate. Interfaces are typically used for derivation: a program implements classes that provide implementations for the interfaces the classes are derived from. Thereafter, objects are created as instances of these derived classes.
The following class definition is an example definition of an interface. In this example, for simplicity of explanation, rather than allowing any class of object to be embedded in its documents, a word processing program allows spreadsheet objects to be embedded. Any spreadsheet object that provides this interface can be embedded, regardless of how the object is implemented. Moreover, any spreadsheet object, whether implemented before or after the word processing program is compiled, can be embedded.
The developer of a spreadsheet program would need to provide an implementation of the interface to allow the spreadsheet objects to be embedded in a word processing document.
When the word processing program embeds a spreadsheet object, the program needs access to the code that implements the interface for the spreadsheet object. To access the class code, each implementation is given a unique class identifier. For example, code implementing a spreadsheet object developed by Microsoft Corporation may have a class identifier of "MSSpreadsheet," while code implementing a spreadsheet object developed by another corporation may have a class identifier of "LTSSpreadsheet." A persistent registry in each computer system is maintained that maps each class identifier to the code that implements the class. Typically, when a spreadsheet program is installed on a computer system, the persistent registry is updated to reflect the availability of that class of spreadsheet objects. So long as a spreadsheet developer implements each function member defined by the interface and the persistent registry is maintained, the word processing program can embed instances of the developer's spreadsheet objects into a word processing document. The word processing program accesses the function members of the embedded spreadsheet objects without regard to who has implemented them or how they have been implemented.
Various spreadsheet developers may wish, however, to implement only certain function members. For example, a spreadsheet developer may not want to implement database support, but may want to support all other function members. To allow a spreadsheet developer to support only some of the function members, while still allowing the objects to be embedded, multiple interfaces for spreadsheet objects are defined. For example, the interfaces IDatabase and IBasic may be defined for a spreadsheet object as follows.
Each spreadsheet developer would implement the IBasic interface and, optionally, the IDatabase interface.
At run time, the word processing program would need to determine whether a spreadsheet object to be embedded supports the IDatabase interface. To make this determination, another interface is defined (that every spreadsheet object implements) with a function member that indicates which interfaces are implemented for the object. This interface is named IUnknown (and referred to as the unknown interface or the object management interface) and is defined as follows.
The IUnknown interface defines the function member (method) QueryInterface. The method QueryInterface is passed an interface identifier (e.g., "IDatabase") in parameter iid (of type REFIID) and returns a pointer to the implementation of the identified interface for the object for which the method is invoked in parameter ppv. If the object does not support the interface, then the method returns a false. The type HRESULT indicates a predefined status, the type REFIID indicates a reference to an interface identifier, and the type ULONG indicates an unsigned long integer.